package com.maoyan.quickdevelop.system.service.Impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.maoyan.quickdevelop.common.StringUtils;
import com.maoyan.quickdevelop.common.constant.HttpStatus;
import com.maoyan.quickdevelop.common.core.AjaxResult;
import com.maoyan.quickdevelop.common.core.domain.dqroom.DqBookInfo;
import com.maoyan.quickdevelop.common.exception.CustomException;
import com.maoyan.quickdevelop.common.utils.DateUtils;
import com.maoyan.quickdevelop.system.mapper.dqroom.DqBookMapper;
import com.maoyan.quickdevelop.system.mapper.dqroom.DqRoomMapper;
import com.maoyan.quickdevelop.system.service.IDqBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
@Slf4j
@Transactional
public class IDqBookServiceImpl implements IDqBookService {
  @Autowired
  private DqBookMapper dqBookMapper;
  @Autowired
  private DqRoomMapper dqRoomMapper;
  @Override
  public Integer checkRoomIsUse(String roomNumber, String bookDate, String endDate) {
    Integer integer = dqBookMapper.checkRoomIsUse(roomNumber, bookDate,endDate);
    return integer;
  }

  @Override
  public Integer bookRoom(String username, String phone, String remarks ,String roomNumber ,String bookDate, String endDate) {
    DateTime bookdate = DateUtil.parseDateTime(bookDate);
    DateTime enddate = DateUtil.parseDateTime(endDate);
    DqBookInfo dqBookInfo = new DqBookInfo();
    dqBookInfo.setRoomId(roomNumber);
    dqBookInfo.setUsername(username);
    dqBookInfo.setPhone(phone);
    dqBookInfo.setRemarks(remarks);
    dqBookInfo.setBookDate(DateUtil.offsetHour(bookdate,-8));
    dqBookInfo.setEndDate(DateUtil.offsetHour(enddate,-8));
    dqBookInfo.setCreateTime(DateUtils.getNowDate());
    dqBookInfo.setUpdateTime(DateUtils.getNowDate());
    int insert = dqBookMapper.insert(dqBookInfo);
    if (insert<=0){
      throw new CustomException("预定失败", HttpStatus.ERROR);
    }
    //预定成功,房间数量减一

    return insert;
  }

  @Override
  public List<DqBookInfo> selectBookInfoByUsername(String username) {
    LambdaQueryWrapper<DqBookInfo> dqBookInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
    dqBookInfoLambdaQueryWrapper.eq(DqBookInfo::getUsername,username);
    List<DqBookInfo> dqBookInfos = dqBookMapper.selectList(dqBookInfoLambdaQueryWrapper);
    if (StringUtils.isEmpty(dqBookInfos)){
      throw new CustomException("未查询到", HttpStatus.NOT_FOUND);
    }
    return dqBookInfos;
  }

  @Override
  public List<String> selectEmptyRoom(Long dqRoomId) {
    List<String> strings = dqBookMapper.selectEmptyRoom(dqRoomId);
    if (StringUtils.isEmpty(strings)){
      throw new CustomException("已经没有空房间了哦",HttpStatus.ERROR);
    }
    return strings;
  }

  @Override
  public List<String> selectUseRoom(Long dqRoomId, String bookDate, String endDate) {
    List<String> strings = dqBookMapper.selectUseRoom(dqRoomId, bookDate, endDate);
    if (StringUtils.isEmpty(strings)) {
      throw new CustomException("这个时间段已经没有房间了哦",HttpStatus.ERROR);
    }
    return strings;
  }
}
